'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2010, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Modules.Blog.Business
Imports System.Globalization
Imports System.Text
Imports System.Text.RegularExpressions

Namespace WillStrohl.Utilities.BlogSiteMap

    Public Class BlogSiteMap
        Implements System.Web.IHttpHandler

#Region " Private Members "

        Private p_PortalId As Integer = Null.NullInteger
        Private p_CategoryId As Integer = Null.NullInteger

#End Region

#Region " Properties "

        Private ReadOnly Property PortalId() As Integer
            Get
                Return Me.p_PortalId
            End Get
        End Property

        Private ReadOnly Property CategoryId() As Integer
            Get
                Return Me.p_CategoryId
            End Get
        End Property

#End Region

        ''' <summary>
        ''' ProcessRequest - this is the default method called by the handler
        ''' </summary>
        ''' <param name="context"></param>
        ''' <remarks></remarks>
        ''' <history>
        ''' [wstrohl] - 20100604 - Making the response end if the portalid is not found
        ''' </history>
        Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
            context.Response.ContentType = "text/xml"

            Dim obj As Object = context.Request.Params("portalid")
            If Not obj Is Nothing Then
                If Regex.IsMatch(obj.ToString, "\d+") Then
                    Me.p_PortalId = Integer.Parse(obj.ToString, Globalization.NumberStyles.Integer)
                End If
            Else
                context.Response.End()
            End If

            obj = context.Request.Params("categoryid")
            If Not obj Is Nothing Then
                If Regex.IsMatch(obj.ToString, "\d+") Then
                    Me.p_CategoryId = Integer.Parse(obj.ToString, Globalization.NumberStyles.Integer)
                End If
            End If

            Dim ctlEntry As New EntryController
            Dim collEntry As New ArrayList
            Dim itmEntry As New EntryInfo

            If Me.CategoryId > Null.NullInteger Then
                collEntry = ctlEntry.ListAllEntriesByCategory(Me.PortalId, Me.CategoryId)
            Else
                collEntry = ctlEntry.ListAllEntriesByPortal(Me.PortalId)
            End If

            Dim sb As New StringBuilder(500)
            With sb
                .Append("<?xml version=""1.0"" encoding=""UTF-8""?>")
                .Append("<urlset xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9"">")
                If Me.PortalId <> Null.NullInteger AndAlso collEntry.Count > 0 Then
                    For Each item As Object In collEntry
                        itmEntry = CType(item, EntryInfo)
                        .Append("<url>")
                        .AppendFormat("<loc>{0}</loc>", EscapeUrl(itmEntry.PermaLink))
                        .Append("<priority>0.80</priority>")
                        .AppendFormat("<lastmod>{0}</lastmod>", DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss+00:00", CultureInfo.InvariantCulture))
                        .Append("<changefreq>daily</changefreq>")
                        .Append("</url>")
                    Next
                End If
                .Append("</urlset>")
            End With

            context.Response.Write(sb.ToString)
        End Sub

#Region " Helper Methods "

        ''' <summary>
        ''' EscapeUrl - this method prevents any XML errors from any characters that might be in the parameter value
        ''' </summary>
        ''' <param name="url"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Function EscapeUrl(ByVal url As String) As String
            If Not String.IsNullOrEmpty(url) Then
                If Not Regex.IsMatch(url, "&amp;") Then
                    url = Regex.Replace(url, "&", "&amp;")
                End If
                If Not Regex.IsMatch(url, "&apos;") Then
                    url = Regex.Replace(url, "'", "&apos;")
                End If
                If Not Regex.IsMatch(url, "&quot;") Then
                    url = Regex.Replace(url, """", "&quot;")
                End If
                If Not Regex.IsMatch(url, "&lt;") Then
                    url = Regex.Replace(url, "<", "&lt;")
                End If
                If Not Regex.IsMatch(url, "&gt;") Then
                    url = Regex.Replace(url, ">", "&gt;")
                End If
            End If

            Return url
        End Function

#End Region

#Region " IHttpHandler Implementation "

        Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
            Get
                Return True
            End Get
        End Property

#End Region

    End Class

End Namespace